package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wit.witmg.entity.TEquip;
import com.wit.witmg.entity.THr;
import com.wit.witmg.entity.TOrg;
import com.wit.witmg.mapper.THrMapper;
import com.wit.witmg.service.ITHrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.service.ITOrgService;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author bob
 * @since 2024-12-20
 */
@Service
@RequiredArgsConstructor
public class THrServiceImpl extends ServiceImpl<THrMapper, THr> implements ITHrService {

    final ITOrgService orgService;

    @Override
    public Object add(HttpServletRequest request, String json) {
//        json="{\"na\":\"郑巨州\",\"belong\":\"董事长\",\"level\":\"9\",\"oid\":\"89\",\"phone\":\"13806607960\",\"pid\":\"1\",\"power\":\"\",\"remark\":\"\"}";
        THr t= JSON.parseObject(json,THr.class);
        if(t.getId()>0){
            boolean bl=  updateById(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_200);
            }
        }else{
            t.setDate(new Date());
            boolean bl= save(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_504);
            }
        }
        return JsonResult.success(t);
    }

    @Override
    public Object get(HttpServletRequest request, Integer oid, Integer pid, Integer id,String pbn) {
//        参数：根据pid从组织结构表中按树状结构返回pid相同的数据，为空时返所有数据，然后把末级结构作为oid从hr表中查询返回人员数据；为id或pbn时返回一条。
        if(id!=null){
            return JsonResult.success(getById(id));
        }else if(pbn!=null){
            QueryWrapper<THr> qw=new QueryWrapper<>();
            qw.eq("pbn",pbn);
            qw.last("limit 1");
            return JsonResult.success(getOne(qw));
        }

        QueryWrapper<TOrg> qwo=new QueryWrapper<>();
        List<TOrg> rootNodes = new ArrayList<>();//父级列表
        List<TOrg> allNodes = orgService.list();
        if(pid!=null){
            qwo.eq("pid",pid);
            rootNodes= orgService.list(qwo);
        }else{
            for (TOrg node : allNodes) {
                if (node.getPid() == null || node.getPid() == 0) {
                    rootNodes.add(node);
                }
            }
        }
        for (TOrg rootNode : rootNodes) {
            buildTree(rootNode, allNodes);
        }
        return JsonResult.success(rootNodes);
    }

    @Override
    public Object del(HttpServletRequest request, Integer id) {
        boolean bl= removeById(id);
        if(bl) {
            return JsonResult.success();
        }
        return JsonResult.error(ErrorCode.ERR_200);
    }

    private void buildTree(TOrg parentNode, List<TOrg> allNodes) {
        List<TOrg> children = new ArrayList<>();
        for (TOrg node : allNodes) {
            if (node.getPid() != null && node.getPid().equals(parentNode.getId())) {
                if(node.getTp()==0){
                    QueryWrapper<THr> qw=new QueryWrapper<>();
                    qw.eq("oid",node.getId());
                    node.setHrs(list(qw));
                }
                children.add(node);
            }
        }
        if (!children.isEmpty()) {
            parentNode.setChildren(children);
            for (TOrg child : children) {
                if(child.getTp()==1) {
                    buildTree(child, allNodes);
                }
            }
        }
    }
}
